/*
 * MemMaster.h
 *
 *  Created on: 2011-7-4
 *      Author: yong
 */

#ifndef MEMPAGEMASTER_H_
#define MEMPAGEMASTER_H_
#include<cstddef>
#include<string>
#include<vector>
#include "Typedef.h"
#include "Page.h"
#include "KeyValuePage.h"
#include "MemPageBoundedBuffer.h"
using namespace std;

namespace distrim
{

/**
 * This class is used to manage memory pages containing records.
 *
 */
template<typename K1, typename V1, typename K2, typename V2>
class MemPageMaster
{
public:
	/**
	 * @precondition:
	 * 1. input file has been splitted into parts(binarized).
	 * 2. maximum number of key-value pairs is known wihin these splits.
	 * @param files:the splitted binary files.
	 * @param numMaxMapperInputPair:
	 * @param numIOThread: number of I/O workers to read splits in.
	 * @param numMapper: number of Mappers to process pages.
	 * @param numMapperInputBuffer: number of buffer pages for I/O workers to put data.
	 * @param numMapperOutputBuffer: number of buffer pages for local reducer to reduce.
	 * @param numLocalReducer: number of local reducers to reduce data, defaults to 1.
	 * @param numValueElem: number of value elements.
	 * @postcondition:
	 */
	MemPageMaster(const vector<string> &files, size_t numMaxMapperInputPair,
			size_t numIOThread, size_t numMapper, size_t numMapperInputBuffer,
			size_t numMapperOutputBuffer, size_t numValueElem = 1,
			size_t numLocalReducer = 1) :
				m_files(files),
				m_kvPages(m_files.size()),
				m_numIOThread(numIOThread),
				m_numMapper(numMapper),
				m_numValueElem(numValueElem),
				m_mapperInputBoundedBuffer(
						(files.size() <= numMapperInputBuffer ? m_mapperInputCacheMode
								= CacheMode::FullCache, files.size()
								: m_mapperInputCacheMode
										= CacheMode::BoundedCache, numMapperInputBuffer),
						numMaxMapperInputPair, m_numValueElem)
	{
		// TODO: initialize vector of page records.
	}
	virtual ~MemPageMaster()
	{
	}

private:
	/**
	 * The name of the file should cache.
	 */
	vector<string> m_files;

	/**
	 *
	 */
	std::vector<KeyValuePage<K1, V1> > m_kvPages;
	/**
	 * Total num of pages of the file.
	 */
	size_t m_numIOThread;

	/**
	 * Num of writters to fill the circled buffers, default to 1.
	 */
	size_t m_numMapper;

	/**
	 * Num of elements of a key-value pair, default to 1, if > 1,
	 *  every pair in the page is a key-mulitivalue.
	 */
	size_t m_numValueElem;
	/**
	 *
	 */
	CacheMode::CacheModeEnum m_mapperInputCacheMode;
	/**
	 *
	 */
	MemPageBoundedBuffer<K1, V1> m_mapperInputBoundedBuffer;
	/**
	 *
	 */
	//MemPageBoundedBuffer<K2, V2> m_mapperOutputBoundedBuffer;


};

}

#endif /* MEMPAGEMASTER_H_ */
